******************************************************************************************************************************************* fragment
**** DO FILE ENROLLMENT DATABASE
*********************************************************************
********************************************************************** fragment
clear all 
set more off
set matsize 11000
cd "C:\Users\sfleit2\Documents\ReclassificationRisk_DoFiles"
********************************************* 
use "C:\Users\sfleit2\Documents\ReclassificationRisk_DoFiles\MultCh_MbrCov_2015.dta", clear
sum 
describe, fullnames
*********************************************

**********************************
local ini = ym(2012,1)
local fin = ym(2015,12)
format mbr_cov_row_eff_dt  %td
format mbr_cov_row_end_dt  %td
gen aux_ini_month= month( mbr_cov_row_eff_dt )
gen aux_ini_year= year( mbr_cov_row_eff_dt )
gen aux_fin_month= month( mbr_cov_row_end_dt )
gen aux_fin_year= year( mbr_cov_row_end_dt )
gen date_num_ini = ym(aux_ini_year, aux_ini_month)
gen date_num_fin = ym(aux_fin_year, aux_fin_month)
bys mbr_sys_id cust_seg_nbr prime_policy: egen min_initial = min(date_num_ini)
bys mbr_sys_id cust_seg_nbr prime_policy: egen max_finish  = max(date_num_fin)


collapse (firstnm) gdr_cd min_initial max_finish  rel_cd (mean) age , by(indv_id mbr_sys_id cust_seg_nbr prime_policy alt_id)
local months "624 636 648 660"
foreach i of local months {
gen month`i'= (min_initial<=`i' & max_finish>=`i')
}
*forvalues i = `ini'/`fin' {
* gen month`i'= (min_initial<=`i' & max_finish>=`i')
*}
reshape long month, i(mbr_sys_id prime_policy) j(date_month)
drop if month==0
gen polnbr = substr( cust_seg_nbr ,3,7)
sort polnbr date_month 
save enrollment_collapsed_2015.dta, replace 


keep if date_month==ym(2015,1)
sort polnbr date_month 
save enrollment_collapsed_2015january.dta, replace 
